package org.zhang.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.zhang.shortlink.common.convention.result.Result;
import org.zhang.shortlink.common.convention.result.Results;
import org.zhang.shortlink.project.dto.req.ShortLinkCreateDTO;
import org.zhang.shortlink.project.dto.req.ShortLinkPageDTO;
import org.zhang.shortlink.project.dto.req.ShortLinkUpdateDTO;
import org.zhang.shortlink.project.dto.resp.ShortLinkCreateVO;
import org.zhang.shortlink.project.dto.resp.ShortLinkGroupCountVO;
import org.zhang.shortlink.project.dto.resp.ShortLinkPageVO;
import org.zhang.shortlink.project.service.ShortLinkService;

import java.io.IOException;
import java.util.List;

/**
 * @author: 张嘉庆
 * @date: 2024/4/9 20:05
 * @description:
 */
@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    /**
     * 创建短链接
     */
    @PostMapping("/api/short-link/link/v1/create")
    public Result<ShortLinkCreateVO> createShortLink(@RequestBody ShortLinkCreateDTO requestParam) {
        return Results.success(shortLinkService.saveShortLink(requestParam));
    }

    /**
     * 分页查询短链接
     */
    @GetMapping("/api/short-link/link/v1/page")
    public Result<IPage<ShortLinkPageVO>> pageShortLink(ShortLinkPageDTO requestParam) {
        return Results.success(shortLinkService.pageShortLink(requestParam));
    }

    /**
     * 根据gid列表获取分组中的短链接数
     */
    @GetMapping("/api/short-link/link/v1/count")
    public Result<List<ShortLinkGroupCountVO>> listCountGroup(@RequestParam("requestParam") List<String> requestParam){
        return Results.success(shortLinkService.listCountGroup(requestParam));
    }

    /**
     * 修改短链接
     */
    @PutMapping("/api/short-link/link/v1/update")
    public Result<Void> listCountGroup(@RequestBody ShortLinkUpdateDTO requestParam){
        return Results.success(shortLinkService.updateShortLink(requestParam));
    }

    /**
     * 短链接跳转
     */
    @GetMapping("/{short-url}")
    public void restoreUrl(@PathVariable("short-url") String shortLink, ServletRequest request, ServletResponse response) throws IOException {
        shortLinkService.restoreUrl(shortLink,request,response);
    }


}
